﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MockupDesigner.Items;

namespace MockupDesigner.Editors.TextEditors.Lists
{
    public class TreeViewEditor : TextEditorBase
    {
        protected TreeViewItem itemToEdit;
        private int maxDepth;

        public override void element_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            string[] items = base.element.Text.Split(new string[] { "\r", "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            List<System.Windows.Controls.TreeViewItem> list = new List<System.Windows.Controls.TreeViewItem>();
            for (int i = 0; i < items.Length; i++)
            {
                int depth = 0;
                if (!items[i].StartsWith("\t"))
                {
                    System.Windows.Controls.TreeViewItem ti = new System.Windows.Controls.TreeViewItem() { Header = items[i], IsExpanded = true };
                    list.Add(ti);
                    AddChildren(ti, items, i + 1, depth);
                }
            }
            this.itemToEdit.Depth = maxDepth;
            this.itemToEdit.NumberOfItems = items.Length;
            this.itemToEdit.ItemsSource = list.ToArray();
        }

        private void AddChildren(System.Windows.Controls.TreeViewItem parent, string[] items, int currentIndex, int depth)
        {
            if (depth > maxDepth)
                maxDepth = depth;
            int index = currentIndex;
            while (index < items.Length)
            {
                if(items[index].LastIndexOf("\t")==depth)
                {
                    System.Windows.Controls.TreeViewItem child = new System.Windows.Controls.TreeViewItem() { Header = items[index].Substring(depth + 1), IsExpanded = true };
                    AddChildren(child,items,currentIndex+1,depth+1);
                    parent.Items.Add(child);
                    index+=1;
                }
                else
                {
                    break;
                }
            }
        }

        public override void element_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            //Nothing to Do
        }

        public override void SetItem(ItemBase itemToEdit)
        {
            base.SetItem(itemToEdit);
            this.itemToEdit = itemToEdit as TreeViewItem;

            StringBuilder sb = new StringBuilder();
            foreach (System.Windows.Controls.TreeViewItem ti in this.itemToEdit.ItemsSource)
            {
                GetTextForTreeViewItem(ti, 0, sb);
            }
            this.element.Text = sb.ToString();
            this.element.AcceptsTab = true;
            this.element.AcceptsReturn = true;
            if (this.element.Height < 150)
            {
                this.element.Height = 150;
            }
            if (this.element.Width < 150)
            {
                this.element.Width = 150;
            }
        }

        private void GetTextForTreeViewItem(System.Windows.Controls.TreeViewItem ti, int depth, StringBuilder sb)
        {
            for (int i = 0; i < depth; i++)
            {
                sb.Append("\t");
            }
            sb.Append(ti.Header);
            sb.AppendLine();
            foreach (System.Windows.Controls.TreeViewItem child in ti.Items)
            {
                GetTextForTreeViewItem(child, depth + 1, sb);
            }
        }
    }
}
